import { Injectable } from '@angular/core';

import { environment } from '../../../environments/environment';

import { HttpHelperService } from '../../common/http/http-helper.service';

import { PageCondition } from '../class/page-condition';
import { BlogArticleCard } from '../class/blog-article-card';
import { BlogArticleCategory } from '../class/blog-article-category';
import { BlogArticle } from '../class/blog-article';


@Injectable({
  providedIn: 'root'
})
export class BlogService {

  constructor(private httpHelper: HttpHelperService) { }

  /**
   * 添加文章分类
   */
  addArticleCategory(category: BlogArticleCategory, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategory";
    this.httpHelper.post(url, category).subscribe((response) => callback && callback(response));
  }

  /** 
   * 删除文章分类
   */
  deleteArticleCategoryById(categoryId: number, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategory/" + categoryId;
    this.httpHelper.delete(url).subscribe((response) => callback && callback(response));
  }

  /**
   *  更新文章分类
   */
  updateArticleCategoryById(categoryId: number, category: BlogArticleCategory, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategory/" + categoryId;
    this.httpHelper.put(url, category).subscribe((response) => callback && callback(response));
  }

  /**
   * 获取文章分类
   */
  queryArticleCategoryById(categoryId: number, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategory/" + categoryId;
    this.httpHelper.get(url).subscribe((response) => callback && callback(response));
  }

  /**
   * 获取所有文章分类
   */
  queryAllArticleCategorys(callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategorys";
    let pageCondition: PageCondition = new PageCondition();
    this.httpHelper.post(url, pageCondition).subscribe((response) => callback && callback(response));
  }

  /**
   * 文章分类是否存在
   */
  existsArticleCategory(categoryName: string, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articleCategory/exists?categoryName=" + categoryName + "&userId=" + environment.userId;
    this.httpHelper.get(url).subscribe((response) => callback && callback(response));
  }

  /** 
   * 添加文章
   */
  addArticle(article: BlogArticle, callback?: (result: any) => void) {
    let url = environment.hostIp + "/article";
    this.httpHelper.post(url, article).subscribe((response) => callback && callback(response));
  }

  /**
   * 删除文章
   */
  deleteArticleById(articleId: number, callback?: (result: any) => void) {
    let url = environment.hostIp + "/article/" + articleId;
    this.httpHelper.delete(url).subscribe((response) => callback && callback(response));
  }

  /** 
   * 更新文章
   */
  updateArticleById(articleId: number, article: BlogArticle, callback?: (result: any) => void) {
    let url = environment.hostIp + "/article/" + articleId;
    this.httpHelper.put(url, article).subscribe((response) => callback && callback(response));
  }

  /**
   * 获取文章
   */
  queryArticleById(articleId: number, callback?: (result: any) => void) {
    let url = environment.hostIp + "/article/" + articleId;
    this.httpHelper.get(url).subscribe((response) => callback && callback(response));
  }

  /** 
   * 分页获取文章
   */
  queryArticlesByPage(currentPage: number, pageSize: number, search: string, callback?: (result: any) => void) {
    let url = environment.hostIp + "/articles?currentPage=" + currentPage + "&pageSize=" + pageSize + "&search=" + search;
    this.httpHelper.get(url).subscribe((response) => callback && callback(response));
  }

  /**
   * 除了指定文章是否存在同名文章
   */
  existsArticle(articleName: string, articleId: number, callback?: (result: any) => void) {
    let url = environment.hostIp + "/article/exists?articleName=" + articleName + "&articleId=" + articleId;
    this.httpHelper.get(url).subscribe((response) => callback && callback(response));
  }
}
